/*
 * Copyright (c) 2024 endless-sky
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef EL_PROTOCOL_AUDIO_COMMAND_HANDLER_H
#define EL_PROTOCOL_AUDIO_COMMAND_HANDLER_H

#include <string>
#include <unordered_map>
#include <nlohmann/json.hpp>
#include <mutex>
#include "command_handler_base.h"  // 添加基类头文件

namespace El {
namespace Protocol {

// 音频文件信息结构
struct AudioFile {
    std::string id;
    std::string name;
    size_t size;
    std::string type;
    int duration;
    std::string description;
    std::string uploadTime;
    std::string lastPlayTime;
    int playCount;
};

// 音频播放任务信息结构
struct PlayTask {
    std::string playId;
    std::string audioId;
    std::string name;
    std::string status;
    int progress;
    int currentTime;
    int volume;
    int repeatCount;
    std::string startTime;
    std::string estimatedEndTime;
};

class AudioCommandHandler : public CommandHandlerBase {  // 继承自基类
public:
    /**
     * 注册音频命令处理函数到处理器映射表
     * 
     * @param handlers 命令处理函数映射表，键为命令名称，值为处理函数
     */
    void RegisterMethodHandlers(
        std::unordered_map<std::string, std::function<nlohmann::json(const std::string &, const nlohmann::json &)>>
            &handlers);

private:
    // 处理音频文件上传
    nlohmann::json HandleAudioUpload(const std::string &id, const nlohmann::json &params);

    // 获取音频文件列表
    nlohmann::json HandleAudioGetList(const std::string &id, const nlohmann::json &params);

    // 播放音频文件
    nlohmann::json HandleAudioPlay(const std::string &id, const nlohmann::json &params);

    // 停止音频播放
    nlohmann::json HandleAudioStop(const std::string &id, const nlohmann::json &params);

    // 删除音频文件
    nlohmann::json HandleAudioDelete(const std::string &id, const nlohmann::json &params);

    // 获取音频播放状态
    nlohmann::json HandleAudioGetPlayStatus(const std::string &id, const nlohmann::json &params);

    // 获取正在播放的音频文件
    nlohmann::json HandleAudioGetNowPlaying(const std::string &id, const nlohmann::json &params);

    // 下载音频文件
    nlohmann::json HandleAudioDownload(const std::string &id, const nlohmann::json &params);

    // 获取扬声器配置
    nlohmann::json HandleSpeakerGetConfig(const std::string &id, const nlohmann::json &params);

    // 设置扬声器配置
    nlohmann::json HandleSpeakerSetConfig(const std::string &id, const nlohmann::json &params);

    // 获取音频输入设备列表
    nlohmann::json HandleAudioGetInputDevices(const std::string &id, const nlohmann::json &params);

private:
    std::map<std::string, std::string> activeFiles_;
    std::mutex activeFilesMutex_;
};

} // namespace Protocol
} // namespace El

#endif // EL_PROTOCOL_AUDIO_COMMAND_HANDLER_H